Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
The 'ono' npm package is a utility for creating and managing errors in JavaScript. It allows you to create custom error messages, wrap existing errors, and add additional context to errors. This can be particularly useful for debugging and logging purposes.
Creating Custom Errors
This feature allows you to create custom error messages. The 'ono' function can be used to generate a new error with a specific message.
const ono = require('ono');
const error = ono('Something went wrong!');
console.error(error);
Wrapping Existing Errors
This feature allows you to wrap existing errors with additional context. This can be useful for adding more information to errors that are caught during execution.
const ono = require('ono');
try {
JSON.parse('invalid JSON');
} catch (err) {
const wrappedError = ono(err, 'Failed to parse JSON');
console.error(wrappedError);
}
Adding Context to Errors
This feature allows you to add additional context to errors, such as custom properties. In this example, a status code is added to the error object.
const ono = require('ono');
const error = ono({ status: 404 }, 'Resource not found');
console.error(error);
console.error(error.status);
The 'verror' package provides similar functionality for creating and managing errors. It allows you to create rich error objects with additional context and stack traces. Compared to 'ono', 'verror' offers more advanced features for error propagation and chaining.
The 'create-error' package is another alternative for creating custom error types. It allows you to define new error classes with custom properties and methods. While 'ono' focuses on wrapping and adding context to errors, 'create-error' is more about defining new error types.
The 'custom-error-generator' package allows you to generate custom error classes with specific properties and methods. It is similar to 'create-error' but offers more flexibility in defining the error structure. Compared to 'ono', it is more focused on error class generation rather than error wrapping and context.
util.format()
or your own custom formatter// Throw an error with formatted message
throw ono("%s is invalid. Must be at least %d characters.", username, minLength);
// Wrap and re-throw an error without losing the original error's message and stack
throw ono(err, "An error occurred while saving your changes");
// Throw an error with custom properties (even a custom method!)
throw ono({code: 413, status: "Invalid data", retry: function() {...}});
// Add custom properties to an existing Error
throw ono(err, {code: 413, status: "Invalid data", retry: function() {...}})
// Any of the above can throw a specific Error subtype instead
throw ono.range(...); // RangeError
throw ono.syntax(...); // SyntaxError
throw ono.reference(...); // ReferenceError
Install using npm:
npm install ono
Then require it in your code:
var ono = require("ono");
Reference ono.js
or ono.min.js
in your HTML:
<script src="https://cdn.rawgit.com/JS-DevTools/ono/master/dist/ono.js"></script>
<script>
var timestamp = new Date().toISOString();
throw ono('This error was thrown at %s', timestamp);
</script>
ono([err], [props], [message, ...])
Creates an Error
object with the given properties.
err
- (optional) An existing error object. This error's message, stack trace, and properties will be appended to the new error.
props
- (optional) An object whose properties will be added to the new error. Properties can be anything, including objects and functions.
message
- (optional) The error message string. If it contains placeholders, then pass each placeholder's value as an additional parameter. See ono.formatter
for more info.
The default ono()
function always creates Error
objects, but you can use any of the following methods to explicitly create the corresponding Error subclass. The method signatures are exactly the same as above.
Method | Error type |
---|---|
ono.error() | Error (this is just an alternate syntax) |
ono.eval() | EvalError |
ono.range() | RangeError |
ono.reference() | ReferenceError |
ono.syntax() | SyntaxError |
ono.type() | TypeError |
ono.uri() | URIError |
ono.formatter
By default, Node's util.format()
function is used (even in browsers) to format error messages and substitute placeholders with their corresponding values. You can set ono.formatter
to a third-party formatter or even your own custom implementation, like this:
ono.formatter = function(message) {
var params = Array.prototype.slice.call(arguments, 1);
return params.reduce(function(message, param, index) {
return message.replace("$" + index, param);
}, message);
}
throw ono("$0 must be greater than $1", 4, 10);
I welcome any contributions, enhancements, and bug-fixes. File an issue on GitHub and submit a pull request.
To build/test the project locally on your computer:
Clone this repo
git clone https://github.com/JS-DevTools/ono.git
Install dependencies
npm install
Run the build script
npm run build
Run the tests
npm test
Ono is 100% free and open-source, under the MIT license. Use it however you want.
FAQs
Throw better errors.
The npm package ono receives a total of 822,796 weekly downloads. As such, ono popularity was classified as popular.
We found that ono demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.